#include "GoldDrawCacheData.h"


GoldDrawCacheData* GoldDrawCacheData::mInstance = NULL;

void GoldDrawCacheData::CreateInstance()
{
	if (mInstance == nullptr)
	{
		mInstance = new GoldDrawCacheData();
	}
}

GoldDrawCacheData* GoldDrawCacheData::GetInstance()
{
	return mInstance;
}

GoldDrawCacheData::GoldDrawCacheData()
	: m_maxCount(0)
{
}

GoldDrawCacheData::~GoldDrawCacheData()
{
	Clear();
}

bool GoldDrawCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void GoldDrawCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool GoldDrawCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/activity/goldDraw.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/activity/goldDraw.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}


void GoldDrawCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		GoldDraw* p = new GoldDraw;
		_safe_check(p);

		p->mId = jm["gold_coin"].asUInt();

		// [1,10]
		Json::Value heroSoul1 = jm["hero_soul1"];
		uint32_t sz = heroSoul1.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mHeroSoul1[i] = heroSoul1[i].asUInt();
		}

		// [[102000100,5,100],[102000200,5,100]]
		Json::Value soulNum1 = jm["soul1_num"];
		sz = soulNum1.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = soulNum1[i];
			uint32_t szz = vv.size();
			UInt32_3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mSoulNum1.push_back(c);
		}

		// 
		Json::Value heroSoul2 = jm["hero_soul2"];
		sz = heroSoul2.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mHeroSoul2[i] = heroSoul2[i].asUInt();
		}

		// [[102000100,5,100],[102000200,5,100]]
		Json::Value soulNum2 = jm["soul1_num"];
		sz = soulNum2.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = soulNum2[i];
			uint32_t szz = vv.size();
			UInt32_3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mSoulNum2.push_back(c);
		}

		// 
		Json::Value heroCard1 = jm["hero_card1"];
		sz = heroCard1.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mHeroCard1[i] = heroCard1[i].asUInt();
		}

		// [[102000100,5,100],[102000200,5,100]]
		Json::Value cardNum1 = jm["card1_num"];
		sz = cardNum1.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = cardNum1[i];
			uint32_t szz = vv.size();
			UInt32_3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mCardNum1.push_back(c);
		}

		// 
		Json::Value heroCard2 = jm["hero_card2"];
		sz = heroCard2.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mHeroCard2[i] = heroCard2[i].asUInt();
		}

		// [[102000100,5,100],[102000200,5,100]]
		Json::Value cardNum2 = jm["card2_num"];
		sz = cardNum2.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = cardNum2[i];
			uint32_t szz = vv.size();
			UInt32_3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mCardNum2.push_back(c);
		}

		// 
		Json::Value buleProp = jm["blue_prop"];
		sz = buleProp.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mBlueProp[i] = buleProp[i].asUInt();
		}

		// [[102000100,5,100],[102000200,5,100]]
		Json::Value blueNum = jm["blue_num"];
		sz = blueNum.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = blueNum[i];
			uint32_t szz = vv.size();
			UInt32_3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mBlueNum.push_back(c);
		}

		// 
		Json::Value greenProp = jm["green_prop"];
		sz = greenProp.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mGreenProp[i] = greenProp[i].asUInt();
		}

		// [[102000100,5,100],[102000200,5,100]]
		Json::Value greenNum = jm["green_num"];
		sz = greenNum.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = greenNum[i];
			uint32_t szz = vv.size();
			UInt32_3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mGreenNum.push_back(c);
		}

		// 
		Json::Value expProp = jm["exp_prop"];
		sz = expProp.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mExpProp[i] = expProp[i].asUInt();
		}

		// [[102000100,5,100],[102000200,5,100]]
		Json::Value expNum = jm["exp_num"];
		sz = greenNum.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = greenNum[i];
			uint32_t szz = vv.size();
			UInt32_3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mGreenNum.push_back(c);
		}

		// [[102000100,5,100],[102000200,5,100]]
		Json::Value goldNum = jm["gold_num"];
		sz = goldNum.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = goldNum[i];
			uint32_t szz = vv.size();
			UInt32_3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mGoldNum.push_back(c);
		}

		// 
		p->mBuyConsume = jm["buy_consume"].asUInt();

		//
		Json::Value giveExp = jm["give_exp"];
		sz = giveExp.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mGiveExp[i] = giveExp[i].asUInt();
		}

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

GoldDraw* GoldDrawCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool GoldDrawCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	GoldDraw* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error5");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);


	_safe_guard_end;

	return true;
}

uint32_t GoldDrawCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}